﻿using System;
using System.Collections.Generic;
using System.Text;
using BL;
using System.Data;
using DB;

namespace DB
{
    public class TopicMapper : IMapper
    {
        List<Entity> lessonsList;
        Topic topic;

        #region IMapper Members

        public List<Entity> LoadAll()
        {
            lessonsList = new List<Entity>();
            DataTable dt = AccessDb.Instance.GetTable("Topics");
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    topic = new Topic();
                    topic.ID = long.Parse(dr["id"].ToString());
                    topic.Subject = Subjects.Instance[int.Parse(dr["subject_id"].ToString())];
                    topic.Theme = dr["themeTopic"].ToString();
                    topic.Hours = Convert.ToInt32(dr["hourTopic"]);
                    lessonsList.Add(topic);
                }
                catch
                {
                    continue;
                }
            }

            return lessonsList;
        }

        public Entity Find(long id)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Insert(Entity e)
        {
            topic = e as Topic;
            DataTable dt = AccessDb.Instance.GetTable("Topics");
            DataRow dr = dt.NewRow();
            dr["id"] = topic.ID;
            dr["subject_id"] = topic.Subject.ID;
            dr["themeTopic"] = topic.Theme;
            dr["hourTopic"] = topic.Hours;
            dt.Rows.Add(dr);
        }

        public void DeleteEntity(Entity e)
        {
            topic = e as Topic;
            DataTable dt = AccessDb.Instance.GetTable("Topics");
            foreach (DataRow dr in dt.Rows)
            {
                if (long.Parse(dr["id"].ToString()) == topic.ID)
                {
                    dr.Delete();
                    return;
                }
            }
        }

        public void UpdateEntity(Entity e)
        {
            topic = e as Topic;
            DataTable dt = AccessDb.Instance.GetTable("Topics");
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    if (long.Parse(dr[0].ToString()) == topic.ID)
                    {
                        dr["id"] = topic.ID;
                        dr["subject_id"] = topic.Subject.ID;
                        dr["themeTopic"] = topic.Theme;
                        dr["hourTopic"] = topic.Hours;
                    }
                }
                catch
                {
                    continue;
                }
            }
        }

        public void ClearAll()
        {
            DataTable dt = AccessDb.Instance.GetTable("Topics");

            foreach (DataRow dr in dt.Rows)
            {
                dr.Delete();
            }
        }

        #endregion
    }
}
